import { updateDocument, deserializeTags } from "../../service/agent-document"; import { getAgentBySlug } from "../../service/agent/agent"; import { requireUser } from "#server/utils/context"; import { validate } from "#server/utils/validation"; import { z } from "zod"; const updateSchema = z.object({ title: z.string().min(1).max(200).optional(), content: z.string().max(50000).optional(), tags: z.array(z.string()).optional(), agentSlug: z.string().nullable().optional(), disabled: z.boolean().optional(), }); export default defineWrappedResponseHandler(async (event) => { const user = await requireUser(event); const idParam = getRouterParam(event, "id"); if (!idParam) return R.throwError(400, "缺少文档 ID", null); const id = parseInt(idParam); if (isNaN(id)) return R.throwError(400, "文档 ID 格式不正确", null); const body = await validate(updateSchema, await readBody(event)); let agentId: number | null | undefined = undefined; if (body.agentSlug !== undefined) { if (body.agentSlug === null) { agentId = null; } else { const agent = await getAgentBySlug(body.agentSlug); agentId = agent?.id ?? null; } } const updated = await updateDocument(id, user.id, { title: body.title, content: body.content, tags: body.tags, agentId, disabled: body.disabled, }); if (!updated) return R.throwError(404, "文档不存在或无权访问", null); return R.success({ ...updated, tags: deserializeTags(updated.tags), }); });